package com.mc.designpattern.factory.factory2;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @author fumengchao
 * @ClassName Factory.java
 * @Description TODO
 * @createTime 2021 0729 14:37:00
 */
public class Factory {


    public  static Api createApi() {
        Properties p = new Properties();
        InputStream in = null;
        try {
            in = Factory.class.getResourceAsStream(
                    "/designpattern/factory.properties");
            p.load(in);
        } catch (IOException e) {
            System.out.println(
                    "装载工厂配置文件出错了，具体的堆栈信息如下：");
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        Api api = null;
        try {
            api = (Api) Class.forName(p.getProperty("factoryclass"))
                    .newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return api;
    }

}